package top.ddandang.blog.controller;


import top.ddandang.blog.bean.GlobalResponse;
import top.ddandang.blog.model.query.PageBean;
import top.ddandang.blog.model.vo.CommentRequestVo;
import top.ddandang.blog.service.CommentService;
import top.ddandang.blog.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author D
 * @since 2020-06-23
 */
@RestController
@Slf4j
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @Resource
    private StatisticsService statisticsService;

    @PostMapping("/insertComment")
    public GlobalResponse insertComment(@RequestBody CommentRequestVo commentRequestVo) {
        commentService.insertComment(commentRequestVo);
        statisticsService.addRedisStatisticsComment(commentRequestVo.getArticleId());
        return GlobalResponse.success().message("评论成功");
    }

    @GetMapping("/listCommentVo")
    public GlobalResponse listCommentVo(int pageNo, int pageSize, int articleId) {
        PageBean pageBean = new PageBean(pageNo, pageSize, articleId);
        Map<String, Object> map = commentService.listCommentVo(pageBean);
        if(map.isEmpty()) {
            return GlobalResponse.failed();
        }else {
            return GlobalResponse.success().data(map);
        }
    }

    @DeleteMapping("/deleteComment")
    public GlobalResponse deleteComment(Integer commentId) {
        int flag = commentService.deleteComment(commentId);
        if(flag == 1) {
            return GlobalResponse.success().message("删除成功");
        }else {
            return GlobalResponse.failed().message("删除失败！");
        }
    }

    @PostMapping("/updateCommentTop")
    public GlobalResponse updateCommentTop (Integer commentId, Boolean top) {
        int flag = commentService.updateCommentTop(commentId, top);
        if(flag == 1) {
            return GlobalResponse.success().message("更新成功");
        }else {
            return GlobalResponse.failed().message("更新失败！");
        }
    }
}

